/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.system.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.system.OperLogDao;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.vo.system.OperLogBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>OperLog</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 15:55:12 
 */
@Repository
public class OperLogDaoImpl extends GenericHibernateDao<OperLog, String>
		implements OperLogDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From OperLog as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see OperLogDao#assembly(OperLogBean)
	 */
	public QueryCondition assembly(OperLogBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see OperLogDao#assemblyCount(OperLogBean)
	 */
	public QueryCondition assemblyCount(OperLogBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see OperLogDao#count(OperLogBean)
	 */
	public int count(OperLogBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see OperLogDao#assemblyNameQuery(OperLogBean)
	 */
	public QueryCondition assermblyNameQuery(OperLogBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see OperLogDao#assemblyNameQueryCount(OperLogBean)
	 */
	public QueryCondition assemblyNameQueryCount(OperLogBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see OperLogDao#findByPage(Page, OperLogBean)
	 */
	public List<OperLog> findByPage(Page<OperLog> page,
			OperLogBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(OperLogBean bean,QueryCondition qc){
		if(bean!=null){
			if(StringUtils.isNotBlank(bean.getKeywords())){
				qc.add("And (obj.memo like ? ", "%"+bean.getKeywords()+"%");
				qc.add("Or obj.loginName like ? ", "%"+bean.getKeywords()+"%");
				qc.add("Or obj.realName like ? )", "%"+bean.getKeywords()+"%");
			}
			if(StringUtils.isNotBlank(bean.getOperObjectId())){
				qc.add("And obj.operObjectId like ? ", bean.getOperObjectId()+"%");
			}
            if(StringUtils.isNotBlank(bean.getOperAct()) && !"0".equals(bean.getOperAct())){ 
				qc.add("And obj.operAct like ? ", "%"+bean.getOperAct()+"%");
			}
            if(StringUtils.isNotBlank(bean.getOperFunc())&& !"0".equals(bean.getOperFunc())){
            	qc.add("And obj.operFunc like ? ", "%"+bean.getOperFunc()+"%");
            }    
            if(bean.getStartTime()!=null && bean.getEndTime() != null){
				qc.add("And obj.operTime >= ? ", bean.getStartTime());
				qc.add("And obj.operTime <= ? ", bean.getEndTime());
			}else if(bean.getStartTime() == null && bean.getEndTime() != null){
				qc.add("And obj.operTime <= ? ", bean.getEndTime());
			}else if(bean.getStartTime() != null && bean.getEndTime() == null){
				qc.add("And obj.operTime >= ? ", bean.getStartTime());
			}           
            
            if(StringUtils.isNotBlank(bean.getOperIp())){
				qc.add("And obj.operIp like ? ", "%"+bean.getOperIp()+"%");
			}
            
		}
		qc.appenOrderBy("obj.operTime desc");
	}
	
	public List<OperLog> getLogInfos( OperLogBean bean) { 
		QueryCondition qc = assembly(bean);	
		qc.append(" order by obj.operTime desc");
		return findByQc(qc);
	}


}